<?php

App::uses('AppController', 'Controller');

/**
 * Groups Controller
 *
 * @property Group $Group
 */
class ClarithromycinController  extends AppController {

    function beforeFilter() {
    	 $this->layout = "template"; 
         $this->Auth->allow('index','confrim');       
        parent::beforeFilter();        
       
    }    
   
    public function index() {     	
        $this->loadModel('Clarithromycin');
         //lay toan bo cau hoi
       if($this->Session->read('nameMember') && $this->Session->read('nameMember')!=""){
           $this->redirect('block');
       }
       
        if ($this->request->is('post')) {
            //check ton tai dong thoi name va location
            $chek = $this->Clarithromycin->find('all',array('conditions'=>array(
                'Clarithromycin.name'=>$this->request->data['Clarithromycin']['name'],
                'Clarithromycin.organ'=>$this->request->data['Clarithromycin']['organ'],
            	'Clarithromycin.faculty'=>$this->request->data['Clarithromycin']['faculty'],
            	'Clarithromycin.email'=>$this->request->data['Clarithromycin']['email'],
            	'Clarithromycin.mobile'=>$this->request->data['Clarithromycin']['mobile']
            	
            )));
            if(count($chek)){
                $this->Session->write('nameMemberlv1', $chek['Clarithromycin']['name']);
                $this->Session->write('idMemberlv1', $this['Clarithromycin']['id']);
                $this->Session->write('logTest',0);
				$this->redirect('confrim');
		 // $this->Session->write('erroMSG',"Account existed, please try again.");
            }else{
	            $this->Clarithromycin->create();      
	            $this->request->data['Clarithromycin']['type']=1;      							
	            if ($this->Clarithromycin->save($this->request->data)) {	            
	                $this->Session->write('nameMemberlv1', $this->request->data['Clarithromycin']['name']);
	                $this->Session->write('idMemberlv1', $this->Clarithromycin->getLastInsertId());
	            	$this->Session->write('logTest',0);
	                $this->Session->delete('erroMSG');
	                $this->redirect('confrim');
	            } else {
	               
	            }
            }
       }
       // $location=$this->Common->getLocation();
         
      //  $this->set(compact('location'));
        $this->render('/Fontend/Clarithromycin/index');        
    }
	public function confrim(){		
		//kiem tra loign
	  if(!$this->Session->read('nameMemberlv1')&& !$this->Session->read('idMemberlv1')){
             $this->redirect('/');
        }
	 if ($this->request->is('post')) {
            $this->Session->write('startTestlv1', date('Y-m-d H:i:s') );
            $this->Session->write('endTest', date('Y-m-d H:i:s') );
            $this->Session->write('logTestLv1',1);
           
            //lay ra thu tu cac cau hoi
            $this->loadModel('Question');
            $dataQuestion = $this->Question->find('all',array(
            	'conditions'=>array(
            		//'Question.time_start <='>date('Y-m-d H:i:s'),
            		//'Question.time_end >='=>date('Y-m-d H:i:s'),
            		'Question.status'=>1,
            	),
            	'fields'=>array(
            		'Question.id',
            		'Question.name'
            	),
            	'order'=>'Question.order asc ,Question.time_end asc'
            	
            ));
            $dataAll = array();
            $i=1;
            foreach ($dataQuestion as $data){
            	if($i==count($dataQuestion)){
            		$j=0;
            		$k=$i-1;
            	}else{
            		$j=1;
            		$k=$i;            	
            	}
            	$dataAll[]=array(
            		'number'=>$i,
            		'nextNumber'=>$k,
            		'stop'=>$j,
            		'id'=>$data['Question']['id'],
            		'name'=>$data['Question']['name'],
            		'ansew'=>$this->getByAnswer($data['Question']['id'])
            	
            	);
            	$i++;
            }
            
            $this->Session->write('dataFull', $dataAll);
            $this->redirect('/startday');
        }
		$this->render('/Fontend/Clarithromycin/confrim'); 
	}
	public function getByAnswer($id = null){
		$this->loadModel('Answer');
		$dataAll = array();
		$data= $this->Answer->find('all',array(
		'conditions'=>array('Answer.id_quyestion'=>$id),
		'fields'=>array(
			'Answer.id',
			'Answer.name',
			'Answer.ruler'
		),
		'order'=>'Answer.id asc'
		));
		foreach ($data as $value){
			$dataAll[]= array(
				'id'=>$value['Answer']['id'],
				'name'=>$value['Answer']['name'],
				'ruler'=>$value['Answer']['ruler']
			);
		}
		
		return $dataAll;
		
	}
	

}
?>
